package com.freestyle.jmssample.rabbitmq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.freestyle.jmssample.api.pojo.SMSBean;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by rocklee on 2022/1/6 11:21
 */
public class SMSUtil {
  private static final ObjectMapper mapper = new ObjectMapper();
  private static AtomicLong inc=new AtomicLong(0);
  public static void reset(){
    inc.set(0);
  }
  public static SMSBean genSMS(){
    long seq=inc.incrementAndGet();
    return new SMSBean("Subject"+seq,"this is a sms message","sender"+seq,"sendto"+seq);
  }
  public static <T> T fromByte(byte[] bytes,Class<T> tClass){
    mapper.constructType(tClass);
    try {
      return mapper.readValue(bytes, tClass);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
  public static byte[] toByte(Object object){
    try {
      return mapper.writeValueAsBytes(object);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
      return null;
    }
  }
  public static void sleep(long ms){
    try {
      Thread.sleep(ms);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
